﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace homework_6th
{
    public partial class Form1 : Form
    {

        String path_1="";
        String path_2="";
        String path_3="";

        FileInfo file_1;
        FileInfo file_2;
        bool[] readError = { false, false, false };
        bool InputDirectionByUser = false;

        /// <summary>
        /// 构造函数作用：
        /// 初始化各文件及文件流操作对象，
        /// 检查.exe文件所在文件夹是否已创建Data文件夹
        /// 初始化三个路径选择框
        /// </summary>
        public Form1()
        {
            InitializeComponent();
            resetCombBox1();
            resetCombBox2();
            resetCombBox3();
            textBox3.Text = "bin\\Debug\\Data\\";
            textBox4.Text = "index.txt";
            DirectoryInfo dir = new DirectoryInfo("Data");
            if (!dir.Exists)
                dir.Create();
            path_3 = "Data\\"+ textBox4.Text;
            label5.Text = "等待修改";

            comboBox1.SelectedIndexChanged += new EventHandler(CombBoxChangedHandler1);
            comboBox2.SelectedIndexChanged += new EventHandler(CombBoxChangedHandler2);
            comboBox3.SelectedIndexChanged += new EventHandler(CombBoxChangedHandler3);
        }

        /// <summary>
        /// 第一个输入文件的combBox的选择事件的handler，
        /// 作用为调用相应的set方法更新文本框所展示的路径，
        /// 并构建相应的文件地址
        /// </summary>
        /// <param name="obj"></param>
        /// <param name="args"></param>
        private void CombBoxChangedHandler1(object obj,EventArgs args)
        {
            try
            {
                if (readError[0])
                {
                    textBox1.Text = "";
                    readError[0] = false;
                }
                setFileText_1();
            }catch(UnauthorizedAccessException)
            {
                textBox1.Text = "无打开文件夹权限";
                readError[0] = true;
                resetCombBox1();
            }
        }
        /// <summary>
        /// 第二个输入文件的combBox的选择事件的handler，
        /// 作用为调用相应的set方法更新文本框所展示的路径，
        /// 并构建相应的文件地址
        /// </summary>
        /// <param name="obj"></param>
        /// <param name="args"></param>
        private void CombBoxChangedHandler2(object obj,EventArgs args)
        {
            try
            {
                if (readError[1])
                {
                    textBox2.Text = "";
                    readError[1] = false;
                }
                setFileText_2();
            }catch(UnauthorizedAccessException)
            {
                textBox2.Text = "无打开文件夹权限";
                readError[1] = true;
                resetCombBox2();
            }
        }
        /// <summary>
        /// 在需要在Data\index.txt以外的文件中存储上述两文件的内容时被调用：
        /// 输出文件/被修改的文件的combBox的选择事件的handler，
        /// 作用为调用相应的set方法更新文本框所展示的路径，
        /// 并构建相应的文件地址
        /// </summary>
        /// <param name="obj"></param>
        /// <param name="args"></param>
        private void CombBoxChangedHandler3(object obj,EventArgs args)
        {
            try
            {
                if (!InputDirectionByUser)
                {
                    InputDirectionByUser = true;
                    textBox4.Text = "";
                    textBox3.Text = "";
                    resetCombBox3();
                }
                if (readError[2])
                {
                    textBox3.Text = "";
                    readError[2] = false;
                }
                setFileText_3();
            }catch(UnauthorizedAccessException)
            {
                textBox3.Text = "无打开文件夹权限";
                readError[2] = true;
                resetCombBox3();
            }
        }
        /// <summary>
        /// 对第一个文件的文本框和地址进行操作，
        /// 可判断当前选择的是文件夹还是文件，
        /// 并更新文本框和选择框的内容，
        /// 并构建第一个文件的完整路径
        /// </summary>
        private void setFileText_1()
        {
            path_1 += comboBox1.Text+'\\';
            DirectoryInfo folder = new DirectoryInfo(path_1);
            FileInfo file = new FileInfo(path_1);
            if (folder.Exists)
            {
                textBox1.Text += comboBox1.Text+'\\';
                comboBox1.Items.Clear();
                foreach(var dir in folder.GetDirectories())
                {
                    comboBox1.Items.Add(dir.Name);
                }
                foreach(var dir in folder.GetFiles())
                {
                    comboBox1.Items.Add(dir.Name);
                }
            }
            else if (file.Exists)
            {
                textBox1.Text += comboBox1.Text;
                comboBox1.Items.Clear();
                file_1 = new FileInfo(path_1);
            }
            else
            {
                textBox1.Text = "不存在该文件夹";
                readError[0] = true;
                resetCombBox1();
                return;
            }
        }
        /// <summary>
        /// 对第一个文件的文本框和地址进行操作，
        /// 可判断当前选择的是文件夹还是文件，
        /// 并更新文本框和选择框的内容，
        /// 并构建第一个文件的完整路径
        /// </summary>
        private void setFileText_2()
        {
            path_2 += comboBox2.Text+'\\';
            DirectoryInfo folder = new DirectoryInfo(path_2);
            FileInfo file = new FileInfo(path_2);
            if (folder.Exists)
            {
                textBox2.Text += comboBox2.Text + '\\';
                comboBox2.Items.Clear();
                foreach (var dir in folder.GetDirectories())
                {
                    comboBox2.Items.Add(dir.Name);
                }
                foreach (var dir in folder.GetFiles())
                {
                    comboBox2.Items.Add(dir.Name);
                }
            }
            else if (file.Exists)
            {
                textBox2.Text += comboBox2.Text;
                comboBox2.Items.Clear();
                file_2 = new FileInfo(path_2);
            }
            else
            {
                textBox2.Text = "不存在该文件夹";
                readError[1] = true;
                resetCombBox2();
                return;
            }
        }
        /// <summary>
        /// 对第一个文件的文本框和地址进行操作，
        /// 可判断当前选择的是文件夹还是文件，
        /// 并更新文本框和选择框的内容，
        /// 并构建第一个文件的完整路径
        /// </summary>
        private void setFileText_3()
        {
            path_3 += comboBox3.Text + '\\';
            DirectoryInfo folder = new DirectoryInfo(path_3);
            FileInfo file = new FileInfo(path_3);
            if (folder.Exists)
            {
                textBox3.Text += comboBox3.Text + '\\';
                comboBox3.Items.Clear();
                foreach (var item in folder.GetDirectories()) 
                {
                    comboBox3.Items.Add(item.Name);
                }
                foreach(var dir in folder.GetFiles())
                {
                    comboBox3.Items.Add(dir.Name);
                }
            }
            else if (file.Exists)
            {
                textBox3.Text += comboBox3.Text;
                comboBox3.Items.Clear();
            }
            else
            {
                textBox3.Text = "不存在该文件夹";
                readError[2] = true;
                resetCombBox3();
                return;
            }
        }
        /// <summary>
        /// 确认按钮的Click事件的handler方法，
        /// 检测输入是否合法，
        /// 将文件路径修改为标准格式，
        /// 调用FolderOperation类的方法进行数据传输
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button1_Click(object sender, EventArgs e)
        {
            if (file_1 == null  || !file_1.Exists )
            {
                textBox1.Text = "未正确输入文件名";
                readError[0] = true;
                resetCombBox1();
                return;
            }
            if(file_2 == null || !file_2.Exists)
            {
                textBox1.Text = "未正确输入文件名";
                readError[0] = true;
                resetCombBox1();
                return;
            }
            String f_1 = checkFilePath(file_1.FullName);
            String f_2 = checkFilePath(file_2.FullName);
            if (f_1.Equals(f_2))
            {
                textBox1.Text = "不能输入相同文件";
                textBox2.Text = "";
                readError[0] = true;
                readError[1] = true;
                resetCombBox1();
                resetCombBox2();
                return;
            }
            FileInfo des = new FileInfo(path_3);
            if(!des.Exists&&textBox4.Text.Length==0)
            {
                textBox4.Text = "未输入新文件完整路径";
                return;
            }
            else if (!des.Exists)
            {
                path_3 += textBox4.Text;
            }
            path_3 = checkFilePath(path_3);
            FolderOperation op = new FolderOperation(f_1, f_2, path_3);
            op.transferInfo();
            label5.Text = "修改完成";
        }
        /// <summary>
        /// 重新输入按钮，更新所有控件的状态
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button2_Click(object sender, EventArgs e)
        {
            textBox1.Clear();
            textBox2.Clear();
            textBox3.Clear();
            resetCombBox1();
            resetCombBox2();
            resetCombBox3ForDataFolder();
            label5.Text = "等待修改";
        }

        /// <summary>
        /// 将文件路径修改为标准格式（去除多余的反斜杠）
        /// </summary>
        /// <param name="path"></param>
        /// <returns></returns>
        private String checkFilePath(String path)
        {
            if (path[path.Length - 1] == '\\')
                return  path.Substring(0, path.Length - 1);
            return path;
        }
        /// <summary>
        /// 文件一相应的控件的刷新方法
        /// </summary>
        private void resetCombBox1()
        {
            path_1 = "";
            comboBox1.Items.Clear();
            comboBox1.Items.Add("C:");
            comboBox1.Items.Add("D:");
            comboBox1.Items.Add("E:");
            comboBox1.Items.Add("F:");
        }
        /// <summary>
        /// 文件二相应的控件的刷新方法
        /// </summary>
        private void resetCombBox2()
        {
            path_2 = "";
            comboBox2.Items.Clear();
            comboBox2.Items.Add("C:");
            comboBox2.Items.Add("D:");
            comboBox2.Items.Add("E:");
            comboBox2.Items.Add("F:");
        }
        /// <summary>
        /// 文件三相应的控件的刷新方法
        /// </summary>
        private void resetCombBox3()
        {
            textBox3.Text = "";
            textBox4.Text = "";
            path_3 = "";
            comboBox3.Items.Clear();
            comboBox3.Items.Add("C:");
            comboBox3.Items.Add("D:");
            comboBox3.Items.Add("E:");
            comboBox3.Items.Add("F:");
        }
        private void resetCombBox3ForDataFolder()
        {
            textBox3.Text = "bin\\Debug\\Data\\";
            textBox4.Text = "index.txt";
            path_3 = "Data\\" + textBox4.Text;
            InputDirectionByUser = false;
            comboBox3.Items.Clear();
            comboBox3.Items.Add("C:");
            comboBox3.Items.Add("D:");
            comboBox3.Items.Add("E:");
            comboBox3.Items.Add("F:");
        }
    }
}
